import { ref } from "vue";
import { getCategoryFilterAPI, getSubCategoryAPI } from "@/apis/category.js";
import { useRoute } from "vue-router";

/**
 *  获取子分类数据
 * @returns
 */
export function useSubCategory() {
    const route = useRoute();

    const subCategoryData = ref({})

    const getSubCategoryData = async () => {
        await getCategoryFilterAPI(route.params.id).then(res => {
            subCategoryData.value = res.result
        })
    }

    getSubCategoryData();

    return {
        subCategoryData
    }
}

/**
 * 获取子分类商品数据
 * @returns
 */
export function useSubCategoryGoods() {
    const goodList = ref([])
    const route = useRoute();
    const disabled = ref(false)

    const reqData = ref({
        categoryId: route.params.id,
        page: 1,
        pageSize: 20,
        sortField: 'publishTime'
    })

    const getGoodList = async () => {
        await getSubCategoryAPI(reqData.value).then(res => {
            goodList.value = res.result.items
        })
    }
    // 返回一个响应对象(Response)
    const getNewPageGoodList = async () => {
        return await getSubCategoryAPI(reqData.value)
    }
    // 默认调用
    getGoodList();

    return {
        goodList,
        reqData,
        getNewPageGoodList,
        disabled
    }
}